<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称" prop="proName">
        <el-input
          v-model="queryParams.proName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:pro_design:add']"
        >新增产品</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:pro_design:export']"
        >导出</el-button>
      </el-col>
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table v-loading="loading" :data="pro_designList" border style="width: 100%" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="序号" align="center" prop="number" />
      <el-table-column label="产品编码" align="center" prop="proSn" />
      <el-table-column label="产品名称" align="center" prop="proName" />
      <el-table-column label="产品类型" align="center">
        <template v-slot="scope">
          {{ proTypeText(scope.row.proType) }}
        </template>
      </el-table-column>
<!--      <el-table-column label="产品状态" align="center" prop="proState" />-->
      <el-table-column label="BOM" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-tag type="success">
            <el-button v-if="scope.row.proBomId==null" size="mini" type="text" icon="el-icon-edit" @click="bomto(scope.row)"
                       v-hasPermi="['mes:pro_design:edit']">编辑</el-button>
            <el-button v-else size="mini" type="text" icon="el-icon-view" @click="bomto(scope.row)"
                       v-hasPermi="['mes:pro_design:edit']">查看</el-button>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="工艺" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-tag type="success">
            <el-button v-if="scope.row.cftId==null" size="mini" type="text" icon="el-icon-edit" @click="craftto(scope.row)"
                       v-hasPermi="['mes:pro_design:edit']">编辑</el-button>
            <el-button v-else size="mini" type="text" icon="el-icon-view" @click="craftto(scope.row)"
                       v-hasPermi="['mes:pro_design:edit']">查看</el-button>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createdBy" />
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSelect(scope.row)"
            v-hasPermi="['mes:pro_design:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:pro_design:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加产品设计对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品名称" prop="proName">
          <el-input v-model="form.proName" placeholder="请输入产品名称" style="width: 50%;" />
        </el-form-item>
        <el-form-item label="产品类型" prop="proType">
          <el-select v-model="form.proType" placeholder="请选择" style="width: 50%;">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入库价格" prop="dsPrice">
          <el-input-number v-model="form.dsPrice" :precision="2" :min="0" style="width: 50%;"></el-input-number>
        </el-form-item>
        <el-form-item label="销售价格" prop="dslePrice">
          <el-input-number v-model="form.dslePrice" :precision="2" :min="0" style="width: 50%;"></el-input-number>
        </el-form-item>
        <el-form-item label="存货单位" prop="unitName">
          <el-select v-model="form.unitId" placeholder="请选择" style="width: 50%;">
            <el-option
              v-for="item in unitOptions"
              :key="item.id"
              :label="item.unitName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看产品设计对话框 -->
    <el-dialog :title="title1" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品名称" prop="proName">
          <el-input v-model="form.proName" :disabled="true" style="width: 50%"/>
        </el-form-item>
        <el-form-item label="产品分类" prop="proType">
<!--          <el-input v-model="form.proType" :disabled="true" />-->
          <el-select v-model="form.proType" placeholder="请选择" :disabled="true" style="width: 50%">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入库价格" prop="dsPrice">
          <el-input-number v-model="form.dsPrice" :precision="2" :min="0" :disabled="true" style="width: 50%"></el-input-number>
        </el-form-item>
        <el-form-item label="销售价格" prop="dslePrice">
          <el-input-number v-model="form.dslePrice" :precision="2" :min="0" :disabled="true" style="width: 50%"></el-input-number>
        </el-form-item>
        <el-form-item label="库存单位" prop="unitName">
          <el-input v-model="form.unitName" controls-position="right" :min="0" :disabled="true" style="width: 50%"></el-input>
        </el-form-item>
<!--        <el-form-item label="创建人" prop="createdBy">-->
<!--          <el-input v-model="form.createdBy" :disabled="true" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="创建时间" prop="createdTime">-->
<!--          <el-input v-model="form.createdTime" :disabled="true" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="更新人" prop="updatedBy">-->
<!--          <el-input v-model="form.updatedBy" :disabled="true" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="更新时间" prop="updatedDate">-->
<!--          <el-input v-model="form.updatedDate" :disabled="true" />-->
<!--        </el-form-item>-->
      </el-form>
      <div class="demo-drawer__foote">
        <el-button type="primary" @click="handleUpdate(form)">编辑</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改产品设计对话框 -->
    <el-dialog :title="title2" :visible.sync="open2" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品名称" prop="proName">
          <el-input v-model="form.proName" placeholder="请输入产品名称" style="width: 50%" />
        </el-form-item>
        <el-form-item label="产品分类" prop="proType">
          <el-select v-model="form.proType" placeholder="请选择" style="width: 50%">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入库价格" prop="dsPrice">
          <el-input-number v-model="form.dsPrice" :precision="2" :min="0" style="width: 50%"></el-input-number>
        </el-form-item>
        <el-form-item label="销售价格" prop="dslePrice">
          <el-input-number v-model="form.dslePrice" :precision="2" :min="0" style="width: 50%"></el-input-number>
        </el-form-item>
        <el-form-item label="存货单位" prop="unitName">
            <el-select v-model="form.unitName" placeholder="请选择" @change="changeunitid()" style="width: 50%">
              <el-option
                v-for="item in unitOptions"
                :key="item.id"
                :label="item.unitName"
                :value="item.unitName">
              </el-option>
            </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listPro_design, getPro_design, delPro_design, addPro_design, updatePro_design } from "@/api/mes/pro_design";
import {listUnit} from "@/api/mes/unit";

export default {
  name: "Pro_design",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品设计表格数据
      pro_designList: [],
      // 新增弹出层标题
      title: "",
      // 新增是否显示弹出层
      open: false,
      // 查看弹出层标题
      title1: "",
      // 查看是否显示弹出层
      open1: false,
      // 修改弹出层标题
      title2: "",
      // 修改是否显示弹出层
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proName: null,
        proType: null,
      },
      // 查询参数
      queryParamsUnit: {
        // pageNum: 10000,
        // pageSize: 10,
        unitName: null,
        unitId: null,
      },
      // 表单参数
      form: {},
      //定义映射

      // 表单校验
      rules: {
      },
      //单位列表
      unitOptions:[],
      // 产品分类下拉框内容
      options: [{
        value: '0',
        label: '成品'
      }, {
        value: '1',
        label: '半成品'
      }, {
        value: '2',
        label: '原材料'
      }],
      // 产品分类映射
      proTypeMap: {
        0: '成品',
        1: '半成品',
        2:'原材料',
      },
      // 拆卡方式下拉框内容
      options1: [{
        value: '0',
        label: '每卡数量'
      }, {
        value: '1',
        label: '总卡片数'
      }]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 改变单位id的值 */
    changeunitid(){
      // 根据 form.unitName 找到对应的存址
      const selectedProduct = this.unitOptions.find(option => option.unitName === this.form.unitName);
      if (selectedProduct) {
        // 更新其他输入框的值
        this.form.unitId = selectedProduct.id
      }
    },
    /** 跳转到bom */
    bomto(row){
          this.$router.push({
            path: '/pro/bom',
            query:{
              productId:row.id,
              proBomId:row.proBomId,
            }});
    },
    /** 跳转到craft */
    craftto(row){
      this.$router.push({
        path:'/pro/craft',
        query:{
          productId:row.id,
          cftId:row.cftId,
        }
      });
    },
    /** 查询单位设计列表 */
    getUnitList() {
      listUnit(this.queryParamsUnit).then(response => {
        this.unitOptions = response.rows;
        console.log(this.unitOptions)
      });
    },
    /** 查询产品设计列表 */
    getList() {
      this.loading = true;
      listPro_design(this.queryParams).then(response => {
        this.pro_designList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.getUnitList();
        this.calculateIndex()
      });
    },
    /** 计算列表编号 */
    calculateIndex() {
      this.pro_designList.forEach((item, index) => {
        this.$set(item, 'number', index + 1);
      });
    },
    //产品分类数字转文字
    proTypeText(proType) {
      return this.proTypeMap[proType] || '未知类型';
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open1 = false;
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        proSn: null,
        proBomId: null,
        unitId: null,
        unitName:null,
        cftId: null,
        proName: null,
        proSource: null,
        proType: null,
        proState: null,
        processNum: null,
        proCycle: null,
        dsPrice: null,
        dslePrice: null,
        isDeleted: null,
        createdBy: null,
        createdTime: null,
        updatedBy: null,
        updatedDate: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增产品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPro_design(id).then(response => {
        this.form = response.data;
        this.form.proType=String(this.form.proType)
        this.open1 = false;
        this.open2 = true;
        this.title2 = "修改产品";
      });
    },
    /** 查看按钮操作 */
    handleSelect(row) {
      this.reset();
      const id = row.id || this.ids
      getPro_design(id).then(response => {
        this.form = response.data;
        console.log(response.data);
        this.form.proType=String(this.form.proType)
        this.open1 = true;
        this.title1 = "查看产品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePro_design(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPro_design(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const proName = row.proName || this.proName;
      this.$modal.confirm('是否确认删除产品"' + proName + '"?').then(function() {
        return delPro_design(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/pro_design/export', {
        ...this.queryParams
      }, `pro_design_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
